// cloudfunctions/getAllOrders/index.js
const cloud = require('wx-server-sdk');
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV });
const db = cloud.database();

/**
 * 云函数入口函数
 * @returns {object} - 返回订单数据或错误信息
 */
exports.main = async (event, context) => {
  try {
    // 直接查询并返回所有订单，按创建时间降序排列
    const orders = await db.collection('orders').orderBy('createTime', 'desc').get();
    return { success: true, data: orders.data };
  } catch (e) {
    console.error('获取订单列表失败', e);
    return { success: false, error: e.message };
  }
};